package com.prs.crm.action.doc;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;

import com.prs.crm.domain.doc.DocType;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.doc.DocTypeService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class DocTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private PaginationSupport pagination;
	private DocTypeService docTypeService;
	private DocType docType;
	private DocType parentDocType;
	private Collection<TreeNode> nodes;
	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;

	private Collection<CodeCommon> codelist;

	@Action("setDocType")
	public String setDocType() {
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"文档类型");
		return SUCCESS;
	}

	@Action(value = "loadDocType", results = { @Result(name = "success", type = "json", params = {
			"root", "docType", "excludeProperties", "childs" }) })
	public String loadDocType() {
		return SUCCESS;
	}

	@Action(value = "loadDocTypeTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadDocTypeTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<DocType> docTypes = this.getDocTypeService()
				.getRootDocTypes();
		for (DocType type : docTypes) {
			TreeNode node = new TreeNode();
			node.setId(type.getId().toString());
			node.setText(type.getName());
			nodes.add(node);
			addChildNode(type, node);
		}
		return SUCCESS;
	}

	private void addChildNode(DocType parent, TreeNode parentNode) {
		if (parent.getChilds() != null) {
			for (DocType type : parent.getChilds()) {
				TreeNode node = new TreeNode();
				node.setId(type.getId().toString());
				node.setText(type.getName());
				parentNode.addChildNode(node);
				addChildNode(type, node);
			}
		}
	}

	@Action(value = "saveDocType", results = {
			@Result(name = "error", type = "chain", location = "setDocType"),
			@Result(name = "success", type = "json", params = { "root",
					"docType", "excludeProperties", "childs,upType" }) })
	public String saveDocType() {
		if (docType.getName() == null || "".equals(docType.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (docType.getDoctypecode() == null
				|| "".equals(docType.getDoctypecode().trim())) {
			addActionError("编码不能为空");
			return ERROR;
		}
		try {
			if (docType.getId() != null) {
				docType.setUpType(parentDocType);
			} else {
				if ("n".equals(Encode)) {
					int EncodeRplice = this.getCodeService().getBackRpliceAll(
							"手动编码", "文档类型", docType.getDoctypecode());
					if (EncodeRplice != 0) {
						this.addActionError("编码重复!请重新输入");
						return ERROR;
					}
				}
				if ("n".equals(Encode)) {
					Code codes = new Code();
					codes.setCodeValue(docType.getDoctypecode());
					codes.setEntityName(this.getClass().getName());
					codes.setCodeType("手动编码");
					codes.setCodeMolde("文档类型");
					this.getCodeService().save(codes);
					docType.setCode(codes);

				} else {
					CodeCommon Vob = this.getCodeCommonService().getVobject(
							codeCommon.getId());
					Code codes = new Code();
					codes.setEntityName(this.getClass().getName());
					codes.setCodeValue(docType.getDoctypecode());
					Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
					this.codeCommonService.saveOrUpdate(Vob);
					codes.setCodeType(Vob.getCodePlan());
					codes.setCodeMolde("文档类型");
					this.getCodeService().save(codes);
					docType.setCode(codes);

				}
			}
			this.getDocTypeService().save(docType);

		} catch (Exception e) {
			// TODO: handle exception
			this.setDocType(null);
		}
		return SUCCESS;
	}

	@Action(value = "removeDocType", results = { @Result(name = "success", type = "json", params = {
			"root", "prompt" }) })
	public String removeDocType() {
		try {
			this.getDocTypeService().remove(docType);
			this.setPrompt("success");
		} catch (Exception e) {
			// TODO: handle exception
			this.setPrompt("error");
		}
		return SUCCESS;
	}

	@Action("viewDocType")
	public String view() {
		return SUCCESS;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public DocTypeService getDocTypeService() {
		return docTypeService;
	}

	public void setDocTypeService(DocTypeService docTypeService) {
		this.docTypeService = docTypeService;
	}

	public DocType getDocType() {
		return docType;
	}

	public void setDocType(DocType docType) {
		this.docType = docType;
	}

	public DocType getParentDocType() {
		return parentDocType;
	}

	public void setParentDocType(DocType parentDocType) {
		this.parentDocType = parentDocType;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

}
